﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WhiteBoard
{
    public partial class ScreenCut1 : Form
    {
        bool IsCutting = false;
        Point p0 = new Point(0, 0);
        Point cursorOffset = new Point(0, 0);
        ScreenCut2 scf = new ScreenCut2();
        

        public ScreenCut1()
        {
            InitializeComponent();
        }

        private void ScreenCut1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (this.btnCut.Visible == false) this.Close();
                else
                {
                    scf.Hide();
                    this.btnCut.Visible = false;
                    this.btnSave.Visible = false;
                }
            }
            else if (e.Button == MouseButtons.Left)
            {
                if (this.btnCut.Visible == false)
                {
                    IsCutting = true;
                    this.btnCut.Visible = true;
                    //ScreenCut2 scf = new ScreenCut2();
                    scf.Show(this);
                    scf.Location = Cursor.Position;
                    p0 = Cursor.Position;
                }
            }
        }

        private void ScreenCut1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (IsCutting == true)
                {
                    cursorOffset.X = Cursor.Position.X - p0.X;
                    cursorOffset.Y = Cursor.Position.Y - p0.Y;
                    if (cursorOffset.X >= 0 && cursorOffset.Y >= 0)
                    {
                        scf.Width = cursorOffset.X;
                        scf.Height = cursorOffset.Y;
                    }
                    
                }
            }
        }

        private void ScreenCut1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (IsCutting == true)
                {
                    if (this.btnCut.Height + this.btnCut.Top + this.btnSave.Height <= this.Height)
                    {
                        this.btnSave.Left = this.btnCut.Left + this.btnCut.Width - 57;
                        this.btnSave.Top = this.btnCut.Top + this.btnCut.Height;

                    }
                    else if (this.btnCut.Top - this.btnSave.Height >= 0)
                    {
                        this.btnSave.Top = this.btnCut.Top - this.btnSave.Height;
                        this.btnSave.Left = this.btnCut.Left;
                    }
                    else if (this.btnCut.Left + this.btnCut.Width + this.btnSave.Width <= this.Width)
                    {
                        this.btnSave.Left = this.btnCut.Left + this.btnCut.Width;
                        this.btnSave.Top = this.btnCut.Top;
                    }
                    else if (this.btnCut.Left - this.btnSave.Width >= 0)
                    {
                        this.btnSave.Left = this.btnCut.Left - this.btnSave.Width;
                        this.btnSave.Top = this.btnCut.Top;
                    }
                    else
                    {
                        this.btnSave.Top = this.btnCut.Top;
                        this.btnSave.Left = this.btnCut.Left;
                    }
                    this.btnSave.Visible = true;
                    
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            
            Bitmap tmpBmp = new Bitmap(this.btnCut.Width,this.btnCut.Height);
            Graphics gr = Graphics.FromImage(tmpBmp);
            gr.CopyFromScreen(this.btnCut.Left,this.btnCut.Top,0,0,this.btnCut.Size);
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Title = "另存为"; 
            saveFile.RestoreDirectory = true;
            saveFile.InitialDirectory = @"E:\程序\WhiteBoard\WhiteBoard\ScreenCutImage";
            saveFile.Filter = "JPEG图像文件(*.jpg)|*.jpg";
            saveFile.OverwritePrompt = true;
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                string path;
                path = saveFile.FileName.ToString();
                Clipboard.SetImage(tmpBmp);
                tmpBmp.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
                this.btnCut.Visible = false;
                this.btnSave.Visible = false;
                scf.Hide();
                gr.Dispose();
                this.Close();
            
          
        }

        private void ScreenCut1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Bitmap tmpBmp = new Bitmap(this.btnCut.Width, this.btnCut.Height);
            Graphics gr = Graphics.FromImage(tmpBmp);
            gr.CopyFromScreen(this.btnCut.Left, this.btnCut.Top, 0, 0, this.btnCut.Size);
            Clipboard.SetImage(tmpBmp);
            this.Close();
        }
    }
}
